﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Cocbook_DAL;
using Cocbook_DTO;

namespace Sale_and_manage
{
    public partial class frmAddNewProject : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmAddNewProject()
        {
            InitializeComponent();
        }
        public Boolean Active = false;

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (txtProjectName.Text == "")
            {
                highlighter1.SetHighlightColor(txtProjectName, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                lblValidate.Text = "Tên hội sách rỗng.";
            }
            else
            {
                if (txtAddress.Text == "")
                {
                    highlighter1.SetHighlightColor(txtAddress, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                    lblValidate.Text = "Địa điểm tổ chức rỗng.";
                    txtAddress.Focus();
                }
                else
                {
                    if (txtStartDate.Text == "")
                    {
                        highlighter1.SetHighlightColor(txtStartDate, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                        lblValidate.Text = "Ngày diễn ra ra rỗng.";
                        txtStartDate.Focus();
                    }
                    else
                    {
                        if (txtEndDate.Text == "" || txtEndDate.Value <= txtStartDate.Value)
                        {
                            highlighter1.SetHighlightColor(txtEndDate, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                            lblValidate.Text = "Ngày kết thúc không hợp lệ.";
                            txtEndDate.Focus();
                        }
                        else
                        {
                            if (txtTableNumber.Text == "")
                            {
                                highlighter1.SetHighlightColor(txtTableNumber, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                                lblValidate.Text = "Số bàn trong hội sách rỗng.";
                                txtTableNumber.Focus();
                            }
                            else
                            {

                                if (MessageBox.Show("Bạn có muốn khởi chạy dự án ngay bây giờ hay không!", "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                {
                                    Active = true;
                                }
                                CreateProject();
                                this.Close();
                            }
                        }
                    }
                }
            }
        }

        public void CreateProject()
        {
            if ((new ProjectDAL()).CreateProject(new Project(0, txtProjectName.Text, txtAddress.Text, DateTime.Parse(txtStartDate.Text), DateTime.Parse(txtEndDate.Text), int.Parse(txtTableNumber.Text), Active)))
            {
                MessageBox.Show("Khởi tạo thành công");
            }
            else
            {

                MessageBox.Show("Khởi tạo thất bại");
            }
        }

        private void txtTable_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }


        private void txtProjectName_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtProjectName, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtAddress_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtAddress, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtStartDate_ValueChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtStartDate, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtEndDate_ValueChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtEndDate, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtTableNumber_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtTableNumber, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void frmAddNewProject_Load(object sender, EventArgs e)
        {
            lblRunningProject.Text = (new ProjectDAL()).GetProjectByStatus().ProjectName;
        }

        
    }
}